<?php
session_start();
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2011 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: servers_admin.php
| CVS Version: 2.00
| Author: ShadowMage
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once "../../maincore.php";
require_once THEMES."templates/admin_header.php";

include INFUSIONS."server_status_panel/server_status_db.php";

if (!checkrights("SVI") || !defined("iAUTH") || $_GET['aid'] != iAUTH) { redirect("../index.php"); }

// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."server_status_panel/locale/".$settings['locale'].".php")) {
    // Load the locale file matching the current site locale setting.
  include INFUSIONS."server_status_panel/locale/".$settings['locale'].".php";
} else {
  // Load the infusion's default locale file.
  include INFUSIONS."server_status_panel/locale/English.php";
}

define("FUSION_LINK", FUSION_SELF.$aidlink);

$servers_action = (isset($_GET['sact'])) ? $_GET['sact'] : '';
opentable($locale['svi_options']);
if($servers_action != '' || $servers_action != null)
{
    print "<a href=\"".FUSION_LINK."\">".$locale['svi_link_home']."</a> | ";
}
print "<a href=\"".FUSION_LINK."&sact=addserver\">".$locale['svi_link_addserver']."</a> |
<a href=\"".FUSION_LINK."&sact=addservice\">".$locale['svi_link_addservice']."</a>";
closetable();
if(isset($_SESSION['message']))
{
    print "<div class=\"admin-message\">".$_SESSION['message']."</div>";
    unset($_SESSION['message']);
}
switch($servers_action)
{
    default:
        opentable($locale['svi_current_servers']);
        $servers_list = dbquery("SELECT * FROM `".DB_SERVERS_TABLE."` ORDER BY `server_name` DESC");
        if(dbcount('(server_id)',DB_SERVERS_TABLE) > 0)
        {
            print "<table width=\"100%\">
            <tr>
                <th align=\"center\" valign=\"middle\" style=\"width: 40%;\">
                    Server Name <br />Server Description
                </th>
                <th align=\"center\" valign=\"middle\" style=\"width: 25%;\">
                    Using Panel
                </th>
                <th align=\"center\" valign=\"middle\" style=\"width: 10%;\">
                    Number of Services
                </th>
                <th align=\"center\" valign=\"middle\" style=\"width: 25%;\">
                    Options
                </th>
            </tr>";
            while($server = dbarray($servers_list))
            {
                $use_panel = ($server['use_panel'] == 1) ? 'tick' : 'cross';
                $service_count = dbcount("(server_id)",DB_SERVICES_TABLE,"server_id='$server[server_id]'");
                print "<tr>
                    <td align=\"center\" valign=\"middle\" style=\"width: 40%;\">
                        $server[server_name]<br />
                        <small>$server[server_description]</small>
                    </td>
                    <td align=\"center\" valign=\"middle\" style=\"width: 25%;\">
                        <a href=\"".FUSION_LINK."&sact=setpanel&type=server&sid=$server[server_id]\"><img style=\"border: none;\" src=\"".INFUSIONS."server_status_panel/images/$use_panel.png\" alt=\"use_panel_$use_panel\" /></a>
                    </td>
                    <td align=\"center\" valign=\"middle\" style=\"width: 10%;\">
                        $service_count
                    </td>
                      <td align=\"center\" valign=\"middle\" style=\"width: 25%;\">
                      <a href=\"".FUSION_LINK."&sact=viewserver&sid=$server[server_id]\">$locale[svi_view_server]</a> | 
                      <a href=\"".FUSION_LINK."&sact=editserver&sid=$server[server_id]\">$locale[svi_edit_server]</a> | 
                      <a href=\"".FUSION_LINK."&sact=deleteserver&sid=$server[server_id]\">$locale[svi_delete_server]</a>
                    </td>
                </tr>";
            }
            print "</table>";
        }else{
            print $locale['svi_errors_e1'];
        }
        closetable();
        break;
    case 'viewserver':
        $sid = (isset($_GET['sid']) && $_GET['sid'] != 0) ? $_GET['sid'] : null;
        if(isset($sid))
        {
            if(dbcount("(server_id)",DB_SERVERS_TABLE,"server_id='$sid'") > 0)
            {
                opentable($locale['svi_view_server']);
                $get_services = dbquery("SELECT * FROM `".DB_SERVICES_TABLE."` WHERE `server_id` = '$sid';");
                if(dbcount("(server_id)",DB_SERVICES_TABLE,"server_id='$sid'") > 0)
                {
                    print "<table width=\"100%\">
                    <tr>
                        <th align=\"center\" valign=\"middle\">Service Name<br />Service Description</th>
                        <th align=\"center\" valign=\"middle\">Service Port</th>
                        <th align=\"center\" valign=\"middle\">Show on Panel</th>
                        <th align=\"center\" valign=\"middle\">Options</th>
                    </tr>";
                    while($services = dbarray($get_services))
                    {
                        $svc_use_panel = ($services['service_use_panel'] == 1) ? 'tick' : 'cross';
                        print "<tr>
                        <td align=\"center\" valign=\"middle\">
                            $services[service_name]<br />
                            <small>$services[service_description]</small>
                        </td>
                        <td align=\"center\" valign=\"middle\">
                            $services[service_port]
                        </td>
                        <td align=\"center\" valign=\"middle\">
                            <a href=\"".FUSION_LINK."&sact=setpanel&type=service&sid=$services[service_id]\"><img style=\"border: none;\" src=\"".INFUSIONS."server_status_panel/images/$svc_use_panel.png\" alt=\"use_panel_$svc_use_panel\" /></a>
                        </td>
                        <td align=\"center\" valign=\"middle\">
                            <a href=\"".FUSION_LINK."&sact=editservice&sid=$services[service_id]\">$locale[svi_edit_service]</a> | 
                            <a href=\"".FUSION_LINK."&sact=deleteservice&sid=$services[service_id]\">$locale[svi_delete_service]</a>
                        </td>
                        </tr>";
                    }
                    print "</table>";
                }else{
                    print $locale['svi_errors_e8'];
                }
                closetable();
            }else{
                $_SESSION['message'] = $locale['svi_errors_e7'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e6'];
            redirect(FUSION_LINK);
        }
        break;
    case 'addserver':
        $add_server = (isset($_POST['addserver'])) ? $_POST['addserver'] : '';
        if(!$add_server)
        {
            opentable($locale['svi_link_addserver']);
            print "<form method=\"post\" action=\"".FUSION_LINK."&sact=addserver\">
            <strong>$locale[svi_asf_sname]</strong>:<br />
            <input type=\"text\" name=\"server_name\" class=\"textbox\" /><br />
            <strong>$locale[svi_asf_sdesc]</strong>:<br />
            <input type=\"text\" name=\"server_desc\" class=\"textbox\" /><br />
            <strong>$locale[svi_asf_usepanel]</strong>:<br />
            <select name=\"use_panel\" class=\"textbox\">
                <option value=\"1\">$locale[svi_asf_opt1]</option>
                <option value=\"0\">$locale[svi_asf_opt2]</option>
            </select><br />
            <input type=\"submit\" class=\"button\" name=\"addserver\" value=\"$locale[svi_link_addserver]\" />
            </form>";
            closetable();
        }else{
            $server_name = stripinput($_POST['server_name']);
            $server_desc = stripinput($_POST['server_desc']);
            $use_panel = (int)($_POST['use_panel']);
            if(empty($server_name))
            {
                $_SESSION['message'] = $locale['svi_errors_e2'];
                redirect(FUSION_LINK."&sact=addserver");
            }
            $add_server = dbquery("INSERT INTO `".DB_SERVERS_TABLE."` (`server_name`,`server_description`,`use_panel`) VALUES ('$server_name','$server_desc','$use_panel');");
            if($add_server)
            {
                $_SESSION['message'] = $locale['svi_server_added'];
                redirect(FUSION_LINK);
            }else{
                $_SESSION['message'] = mysql_error();
                redirect(FUSION_LINK);
            }
        }
        break;
    case 'addservice':
        $add_service = (isset($_POST['addservice'])) ? $_POST['addservice'] : '';
        $count_servers = dbcount('(server_id)',DB_SERVERS_TABLE);
        if($count_servers > 0)
        {
            if(!$add_service)
            {
                opentable($locale['svi_link_addservice']);
                print "<form method=\"post\" action=\"".FUSION_LINK."&sact=addservice\">
                <strong>$locale[svi_asf_sename]</strong>:<br />
                <input type=\"text\" name=\"service_name\" class=\"textbox\" /><br />
                <strong>$locale[svi_asf_sedesc]</strong>:<br />
                <input type=\"text\" name=\"service_description\" class=\"textbox\" /><br />
                <strong>$locale[svi_asf_seport]</strong>:<br />
                <input type=\"text\" name=\"service_port\" class=\"textbox\" /><br />";
                print "<strong>$locale[svi_asf_sesid]</strong>:<br />
                <select name=\"server_id\">";
                $get_servers = dbquery("SELECT * FROM `".DB_SERVERS_TABLE."` ORDER BY `server_id` DESC");
                while($server = dbarray($get_servers)){
                    print "<option value=\"$server[server_id]\">$server[server_name]</option>\n";
                }
                print "</select><br />
                <input type=\"submit\" name=\"addservice\" value=\"$locale[svi_link_addservice]\" class=\"button\" />
                </form>";
                closetable();
            }else{
                $service_name = stripinput($_POST['service_name']);
                $service_desc = stripinput($_POST['service_description']);
                $service_port = (int)($_POST['service_port']);
                $server_id = (int)($_POST['server_id']);
                if(empty($service_name))
                {
                    $_SESSION['message'] = $locale['svi_errors_e4'];
                    redirect(FUSION_LINK."&sact=addservice");
                }
                if(empty($service_port) || $service_port == 0)
                {
                    $_SESSION['message'] = $locale['svi_errors_e5'];
                    redirect(FUSION_LINK."&sact=addservice");
                }
                $add_service = dbquery("INSERT INTO `".DB_SERVICES_TABLE."` (`server_id`,`service_name`,`service_description`,`service_port`,`service_use_panel`) VALUES ('$server_id','$service_name','$service_desc','$service_port','1');");
                if($add_service)
                {
                    $_SESSION['message'] = $locale['svi_service_added'];
                    redirect(FUSION_LINK);
                }else{
                    $_SESSION['message'] = mysql_error();
                    redirect(FUSION_LINK);
                }
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e3'];
            redirect(FUSION_LINK."&sact=addserver");
        }
        break;
    case 'setpanel':
        $sid = (isset($_GET['sid']) && $_GET['sid'] != 0) ? $_GET['sid'] : null;
        $type = (isset($_GET['type'])) ? $_GET['type'] : null;
        if(!is_null($type))
        {
            if(in_array($type, array('server','service')))
            {
                if($sid)
                {
                    switch($type)
                    {
                        case 'server':
                            if(dbcount("(server_id)",DB_SERVERS_TABLE) > 0)
                            {
                                $get_server = dbquery("SELECT * FROM `".DB_SERVERS_TABLE."` WHERE `server_id` = '$sid';");
                                $server_data = dbarray($get_server);
                                if($server_data['use_panel'] == 1)
                                {
                                    $set_server_panel = dbquery("UPDATE `".DB_SERVERS_TABLE."` SET `use_panel` = '0' WHERE `server_id` = '$sid';");
                                }else{
                                    $set_server_panel = dbquery("UPDATE `".DB_SERVERS_TABLE."` SET `use_panel` = '1' WHERE `server_id` = '$sid';");
                                }
                                if($set_server_panel)
                                {
                                    $message = ($server_data['use_panel'] != 1) ? $locale['svi_server_panel_used'] : $locale['svi_server_panel_unused'];
                                    $_SESSION['message'] = sprintf($message,$server_data['server_name']);
                                    redirect(FUSION_LINK);
                                }else{
                                    $_SESSION['message'] = mysql_error();
                                    redirect(FUSION_LINK);
                                }
                            }else{
                                $_SESSION['message'] = $locale['svi_errors_e7'];
                                redirect(FUSION_LINK);
                            }
                            break;
                        case 'service':
                            if(dbcount("(service_id)",DB_SERVICES_TABLE) > 0)
                            {
                                $get_service = dbquery("SELECT * FROM `".DB_SERVICES_TABLE."` WHERE `service_id` = '$sid';");
                                $service_data = dbarray($get_service);
                                if($service_data['service_use_panel'] == 1)
                                {
                                    $set_service_panel = dbquery("UPDATE `".DB_SERVICES_TABLE."` SET `service_use_panel` = '0' WHERE `service_id` = '$sid';");
                                }else{
                                    $set_service_panel = dbquery("UPDATE `".DB_SERVICES_TABLE."` SET `service_use_panel` = '1' WHERE `service_id` = '$sid';");
                                }
                                if($set_service_panel)
                                {
                                    $server_data = dbarray(dbquery("SELECT * FROM `".DB_SERVERS_TABLE."` WHERE `server_id` = '$service_data[server_id]';"));
                                    $message = ($service_data['service_use_panel'] != 1) ? $locale['svi_service_panel_used'] : $locale['svi_service_panel_unused'];
                                    $_SESSION['message'] = sprintf($message,$service_data['service_name'], $server_data['server_name']);
                                    redirect(FUSION_LINK);
                                }else{
                                    $_SESSION['message'] = mysql_error();
                                    redirect(FUSION_LINK);
                                }
                            }else{
                                $_SESSION['message'] = $locale['svi_errors_e9'];
                                redirect(FUSION_LINK);
                            }
                            break;
                    }
                }else{
                    $_SESSION['message'] = $locale['svi_errors_e6'];
                    redirect(FUSION_LINK);
                }
            }else{
                $_SESSION['message'] = $locale['svi_errors_e12'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e11'];
            redirect(FUSION_LINK);
        }
        break;
    case 'editserver':
        $sid = (int)($_GET['sid']);
        $edit_server = (isset($_POST['editserver'])) ? $_POST['editserver'] : null;
        if(isset($sid) && $sid != 0)
        {
            $get_server = dbquery("SELECT * FROM `".DB_SERVERS_TABLE."` WHERE `server_id` = '$sid';");
            if(dbcount("(server_id)",DB_SERVERS_TABLE,"server_id='$sid'") > 0)
            {
                if(!isset($edit_server))
                {
                    $server_data = dbarray($get_server);
                    opentable($locale['svi_edit_server']);
                    print "<form method=\"post\" action=\"".FUSION_LINK."&sact=editserver&sid=$sid\">
                    <strong>Note</strong>: If you would like this server to use the side panel, please use the icon indicators in the home area.<br />
                    <strong>$locale[svi_asf_sdesc]</strong>:<br />
                    <input type=\"text\" name=\"server_description\" class=\"textbox\" value=\"$server_data[server_description]\" /><br />
                    <strong>$locale[svi_asf_usepanel]</strong>:<br />
                    <select name=\"use_panel\">";
                    if($server_data['use_panel'] == 1)
                    {
                        print "<option value=\"1\" selected=\"selected\">$locale[svi_asf_opt1]</option>";
                    }else{
                        print "<option value=\"1\">$locale[svi_asf_opt2]</option>";
                    }
                    if($server_data['use_panel'] == 0)
                    {
                        print "<option value=\"0\" selected=\"selected\">$locale[svi_asf_opt2]</option>";
                    }else{
                        print "<option value=\"0\">$locale[svi_asf_opt2]</option>";
                    }
                    print "</select><br />
                    <input type=\"submit\" name=\"editserver\" value=\"$locale[svi_edit_server]\" class=\"button\" />
                    </form>";
                    closetable();
                }else{
                    $server_description = stripinput($_POST['server_description']);
                    $use_panel = (int)($_POST['use_panel']);
                    $update_server = dbquery("UPDATE `".DB_SERVERS_TABLE."` SET `use_panel` = '$use_panel', `server_description` = '$server_description' WHERE `server_id` = '$sid';");
                    if($update_server)
                    {
                        $_SESSION['message'] = $locale['svi_server_updated'];
                        redirect(FUSION_LINK."&sact=editserver");
                    }else{
                        $_SESSION['message'] = mysql_error();
                        redirect(FUSION_LINK."&sact=editserver");
                    }
                }
            }else{
                $_SESSION['message'] = $locale['svi_errors_e7'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e6'];
            redirect(FUSION_LINK);
        }
        break;
    case 'editservice':
        $sid = (int)$_GET['sid'];
        $edit_service = (isset($_POST['edit_service'])) ? $_POST['edit_service'] : '';
        if(isset($sid) && $sid != 0)
        {
            $get_service = dbquery("SELECT * FROM `".DB_SERVICES_TABLE."` WHERE `service_id` = '$sid';");
            if(dbcount("(service_id)",DB_SERVICES_TABLE,"service_id='$sid'") > 0)
            {
                $service_data = dbarray($get_service);
                if(!$edit_service)
                {
                    opentable($locale['svi_edit_service']);
                    print "<form method=\"post\" action=\"".FUSION_LINK."&sact=editservice&sid=$sid\">
                    <strong>Note</strong>: If you would like to change whether this service is on the panel or not, use the icon in the '$locale[svi_view_server]' page.<br /><br />
                    <strong>Service Name</strong>:<br />
                    <input type=\"text\" class=\"textbox\" name=\"service_name\" value=\"$service_data[service_name]\" /><br />
                    <strong>$locale[svi_asf_sedesc]</strong>:<br />
                    <input type=\"text\" name=\"service_description\" class=\"textbox\" value=\"$service_data[service_description]\" /><br />
                    <strong>$locale[svi_asf_seport]</strong>:<br />
                    <input type=\"text\" name=\"service_port\" class=\"textbox\" value=\"$service_data[service_port]\" /><br />
                    <strong>$locale[svi_asf_sesid]</strong>:<br />
                    <select name=\"server_id\">";
                    $get_servers = dbquery("SELECT * FROM `".DB_SERVERS_TABLE."`");
                    while($server = dbarray($get_servers))
                    {
                        if($server['server_id'] == $service_data['server_id'])
                        {
                            print "<option selected=\"selected\" value=\"$server[server_id]\">$server[server_name]</option>\n";
                        }else{
                            print "<option value=\"$server[server_id]\">$server[server_name]</option>\n";
                        }
                    }
                    print "</select><br />
                    <input type=\"submit\" name=\"edit_service\" value=\"".$locale['svi_edit_service']."\" class=\"button\" /><br />
                    </form>";
                    closetable();
                }else{
                    $service_desc = stripinput($_POST['service_description']);
                    $service_port = (int)$_POST['service_port'];
                    $server = (int)$_POST['server_id'];
                    $service_name = stripinput($_POST['service_name']);
                    if(empty($service_name) || empty($service_port))
                    {
                        redirect(FUSION_LINK."&sact=editservice&sid=$sid");
                    }
                    $update_service = dbquery("UPDATE `".DB_SERVICES_TABLE."` SET `service_name` = '$service_name', `service_port` = '$service_port', `server_id` = '$server', `service_description` = '$service_desc' WHERE `service_id` = '$sid';");
                    if($update_service)
                    {
                        $_SESSION['message'] = sprintf($locale['svi_service_updated'],$servce_data['service_name']);
                        redirect(FUSION_LINK);
                    }else{
                        $_SESSION['message'] = "<span style=\"color: red;\"><strong>ERROR</strong></span>: ".mysql_error();
                        redirect(FUSION_LINK);
                    }
                }
            }else{
                $_SESSION['message'] = $locale['svi_errors_e9'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e10'];
            redirect(FUSION_LINK);
        }
        break;
    case 'deleteserver':
        $sid = (int)$_GET['sid'];
        $deleteserver = (isset($_POST['deleteserver'])) ? $_POST['deleteserver'] : '';
        if(isset($sid))
        {
            if(dbcount("(server_id)",DB_SERVERS_TABLE) > 0)
            {
                if(!$deleteserver)
                {
                    opentable($locale['svi_delete_server']);
                    print "<form method=\"post\" action=\"".FUSION_LINK."&sact=deleteserver&sid=$sid\">
                    <strong>Note</strong>: Pressing the following button will remove the server and all services associated with the server.<br />
                    <input type=\"submit\" name=\"deleteserver\" value=\"".$locale['svi_delete_server']."\" class=\"button\" />&nbsp;&nbsp;
                    <a onclick=\"javascript:history.go(-1);\">Go Back</a>
                    </form>";
                    closetable();
                }else{
                    $delete_server = dbquery("DELETE FROM `".DB_SERVERS_TABLE."` WHERE `server_id` = '$sid';");
                    $delete_associated_services = dbquery("DELETE FROM `".DB_SERVICES_TABLE."` WHERE `server_id` = '$sid';");
                    if($delete_server & $delete_associated_services)
                    {
                        $_SESSION['message'] = $locale['svi_server_deleted'];
                        redirect(FUSION_LINK);
                    }else{
                        $_SESSION['message'] = mysql_error();
                        redirect(FUSION_LINK);
                    }
                }
            }else{
                $_SESSION['message'] = $locale['svi_errors_e7'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e6'];
            redirect(FUSION_LINK);
        }
        break;
    case 'deleteservice':
        $sid = (int)$_GET['sid'];
        $deleteservice = (isset($_POST['deleteservice'])) ? $_POST['deleteservice'] : '';
        if(isset($sid))
        {
            if(dbcount("(server_id)",DB_SERVERS_TABLE) > 0)
            {
                if(!$deleteserver)
                {
                    opentable($locale['svi_delete_server']);
                    print "<form method=\"post\" action=\"".FUSION_LINK."&sact=deleteservice&sid=$sid\">
                    <strong>Note</strong>: Pressing the following button will remove the service.<br />
                    <input type=\"submit\" name=\"deleteservice\" value=\"".$locale['svi_delete_service']."\" class=\"button\" />&nbsp;&nbsp;
                    <a onclick=\"javascript:history.go(-1);\">Go Back</a>
                    </form>";
                    closetable();
                }else{
                    $delete_service = dbquery("DELETE FROM `".DB_SERVICES_TABLE."` WHERE `service_id` = '$sid';");
                    if($delete_service)
                    {
                        $_SESSION['message'] = $locale['svi_service_deleted'];
                        redirect(FUSION_LINK);
                    }else{
                        $_SESSION['message'] = mysql_error();
                        redirect(FUSION_LINK);
                    }
                }
            }else{
                $_SESSION['message'] = $locale['svi_errors_e9'];
                redirect(FUSION_LINK);
            }
        }else{
            $_SESSION['message'] = $locale['svi_errors_e10'];
            redirect(FUSION_LINK);
        }
        break;
}
require_once THEMES."templates/footer.php";
?>
